package Obj;

import java.util.ArrayList;
import java.util.Scanner;

// 飞机接口
interface IPlane {
    public void takeOff();
    public void land();
    public void flying();
}

// 飞机（父类）
class Plane implements IPlane {
    private String name;
    private String model;

    public Plane(String name, String model) {
        this.name = name;
        this.model = model;
    }

    public String getName() {
        return this.name;
    }
    public String getModel() {
        return this.model;
    }

    public void takeOff() {
        System.out.println("[" + this.name + this.model + "]起飞");
    }
    public void land() {
        System.out.println("[" + this.name + this.model + "]降落");
    }
    public void flying() {
        System.out.println("[" + this.name + this.model + "]正常飞行");
    }
}

// 直升机（子类）
class Helicopter extends Plane {
    public Helicopter(String name, String model) {
        super(name, model);
    }
}

// 战斗机（子类）
class Warcraft extends Plane {
    private ArrayList <Integer> shellList = new ArrayList<>();

    public Warcraft(String name, String model) {
        super(name, model);
    }

    public void setShell(int num) {
        // this.shellList = new ArrayList<>(num);
        for (int i = 0; i < num; ++i)
            shellList.add(i);
        System.out.println("现已装载弹药数量：" + shellList.size());
    }
    public void throwShell(int num) {
        if (shellList.size() < num)
            System.out.println("发射失败!弹药不足!");
        else {
            for (int i = 0; i < num; ++i)
                shellList.remove(0);
            System.out.println(num + "颗弹药发射"+" 剩余弹药：" + shellList.size());
        }
    }
}

// 客机（子类）
class Airliner extends Plane {
    private ArrayList <Passenger> list = new ArrayList<>(128);

    public Airliner(String name, String model) {
        super(name, model);
    }

    public void getNum() {
        System.out.println("机上乘客 " + list.size() + " 人");
    }

    public void board(Passenger p) {
        if (list.size() < 128) {
            System.out.println("欢迎乘客[" + p.getName() + "]上机");
            list.add(p);
        } else {
            System.out.println("Warning!已经达到核载数目!");
        }
    }
    public void deplane() {
        while (!list.isEmpty()) {
            System.out.println("乘客" + list.get(0).getName() + "下机,欢迎再次选择ANZ航空!");
            list.remove(0);
        }
    }
}

// 乘客类
class Passenger {
    private String name;
    private String ID;

    public Passenger(String name, String ID) {
        this.name = name;
        this.ID = ID;
    }

    public String getName() {
        return this.name;
    }

    public String getID() {
        return this.ID;
    }
}

// 机场类
class Airport {
    public void controlPlane(IPlane plane, int commandCode) {
        if (plane instanceof Airliner) {
            Airliner a = (Airliner)plane;
            switch (commandCode) {
                case 1: a.takeOff(); break;
                case 2: a.land(); break;
                case 3: a.flying(); break;
                case 4: 
                        Passenger p1 = new Passenger("特朗普", "0070");
                        Passenger p2 = new Passenger("拜登", "0075");
                        a.board(p1); 
                        a.board(p2); 
                        break;
                case 5:
                        a.deplane();
                        break;
                default: System.out.println("请发出正确指令!"); break;
            } 
        } else if (plane instanceof Warcraft) {
            Warcraft w = (Warcraft)plane;
            switch (commandCode) {
                case 1: w.takeOff(); break;
                case 2: w.land(); break;
                case 3: w.flying(); break;
                case 4: 
                        System.out.println("战斗机准备起飞，请配置炮弹数：");
                        w.setShell(Main.scanner.nextInt());
                        break;
                case 5:
                        System.out.println("请选择发射的炮弹数：");
                        w.throwShell(Main.scanner.nextInt());
                        break;
                default: System.out.println("请发出正确指令!"); break;
            }
        }
    }
}


public class Main {
    public static Scanner scanner = new Scanner(System.in);

    private static void ask() {
        System.out.println("请选择想要查看的飞机类型：");
        System.out.println("0.退出 1.直升机 2.客机 3.战斗机");
    }
    private static void report(Plane plane) {
        System.out.println("现在已选择：" + plane.getName() + plane.getModel());
    }
    
    public static void main(String[] args) {
        Airport airport = new Airport();
        Helicopter h = new Helicopter("中国：直", "7");
        Airliner a = new Airliner("波音", "747");
        Warcraft w = new Warcraft("战斗机", "F-14");

        ask();
        int flag = 1;
        while (flag != 0) {
            ask();
            flag = scanner.nextInt();
            switch(flag) {
                case 1: 
                    report(h);
                    System.out.println("更多内容敬请期待!");
                    break;
                case 2:
                    report(a);
                    System.out.println("客机功能如下:1.起飞 2.降落 3.平稳飞行 4.上机 5.下机");
                    airport.controlPlane(a, scanner.nextInt());
                    break;
                case 3:
                    report(w);
                    System.out.println("客机功能如下:1.起飞 2.降落 3.平稳飞行 4.装填炮弹 5.发射炮弹");
                    airport.controlPlane(w, scanner.nextInt());
                    break;
            }
        }
        scanner.close();
        System.exit(0);
    }
}
